package com.ticket.sass.admin.dto.input.douyin;

/**
 * @author ywb
 * @date 2024-07-19 11:31
 */

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.Valid;
import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Data
@ApiModel(value="DouyinGoodExchangeSetting对象", description="兑换设置输入数据")
public class DouyinGoodExchangeSettingInput implements Serializable {
    @ApiModelProperty(value = "商品ID", required = true)
    private String productId;
    
    @Valid
    @ApiModelProperty(value = "兑换设置列表", required = true)
    private List<ExchangeSetting> exchangeSettings;
    
    // 默认构造函数
    public DouyinGoodExchangeSettingInput() {}
    
    @Data
    @ApiModel(value="ExchangeSetting对象", description="兑换设置详细信息")
    public static class ExchangeSetting implements Serializable {
        @NotNull(message = "优惠券ID不能为空")
        @ApiModelProperty(value = "优惠券ID", required = true)
        private Long couponId;
        
        @ApiModelProperty(value = "数量")
        private Integer number;
        
        @ApiModelProperty(value = "有效时长")
        private Integer effectiveDuration;
        
        // 默认构造函数
        public ExchangeSetting() {}
    }
    
    
    /**
     * 校验exchangeSettings中的couponId是否唯一。
     * @return 如果couponId唯一返回true，否则返回false
     */
    @AssertTrue(message = "优惠券ID不能重复")
    public boolean validateUniqueCouponIds() {
        Set<Long> couponIds = new HashSet<>();
        for (ExchangeSetting setting : exchangeSettings) {
            if (!couponIds.add(setting.getCouponId())) {
                // 如果couponId已存在，则返回false
                return false;
            }
        }
        // 所有couponId都唯一，返回true
        return true;
    }
}

